package com.itheima.service.impl;

import com.itheima.mapper.ChatMapper;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.Chat;
import com.itheima.pojo.Friend;
import com.itheima.pojo.Group;
import com.itheima.pojo.User;
import com.itheima.service.ChatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
import java.util.Collections;
import java.util.List;

@Slf4j
@org.springframework.stereotype.Service
public class ChatServiceImpl implements ChatService {


    @Autowired
    private ChatMapper chatMapper;
    @Autowired
    private UserMapper userMapper;
    private String imagePath = "C:\\Users\\高书恒\\Desktop\\group.jpg";


    /**
     * @param groupName
     * @param userName
     * @return
     */
    public Integer luanchChat(String groupName, String userName) throws IOException {
        Group group = chatMapper.getGroupByName(groupName);
        if (group != null) return -1;
        chatMapper.insertGroup(groupName, userName);

        String avatar = "data:image/jpg;base64," + Base64.getEncoder().encodeToString(Files.readAllBytes(Path.of(imagePath)));

        userMapper.insertAvatar(groupName, avatar);
        return 1;
    }

    /**
     * 根据ID查询员工
     *
     * @param userName
     * @return
     */
    public List<Group> getGroup(String userName) {
        List<Group> groupList = chatMapper.groupList(userName);
        return groupList;
    }

    /**
     * 根据ID查询员工
     *
     * @param userName
     * @return
     */
    @Override
    public List<Friend> getFriend(String userName) {
        List<Friend> friendList = chatMapper.friendList(userName);
        return friendList;
    }


    /**
     * @param userName
     * @param sendContent
     */
    public void storeMsg(String userName, String receiverName, String sendContent) {
        chatMapper.storeMsg(userName, receiverName, sendContent);
    }


    /**
     * @param userName
     * @param chatName
     * @return
     */
    public Chat getContentByName(String userName, String chatName) {
        User user = chatMapper.getUserByName(chatName);
        if(user != null) {
            Chat userChat = chatMapper.getContentByName(userName, chatName);
            Chat otherChat = chatMapper.getContentByName(chatName, userName);
            if (userChat == null && otherChat == null) return null;
            if (userChat == null) return otherChat;
            else if (otherChat == null) return userChat;
            else return userChat.getChatId() > otherChat.getChatId() ? userChat : otherChat;
        }else{
            return chatMapper.getGroupMsg(chatName);
        }
    }


    /**
     *
     * @param userName
     * @param chatName
     * @return
     */
    public Chat getMsg(String userName, String chatName) {
        User user = chatMapper.getUserByName(chatName);
        if(user != null) return chatMapper.getMsg(userName, chatName);
        else return chatMapper.getGroupMsg(chatName);
    }

    /**
     * @param friendName
     * @param userName
     * @return
     */
    public Integer addFriend(String friendName, String userName) {
        User user = chatMapper.getUserByName(friendName);
        if (user == null) return -1;
        Friend friend = chatMapper.getFriendByName(friendName, userName);
        if (friend != null) return 0;
        chatMapper.addFriend(friendName, userName);
        chatMapper.addFriend(userName, friendName);
        return 1;
    }

    /**
     * @param groupName
     * @param userName
     * @return
     */
    public Integer addGroup(String groupName, String userName) {
        Group group = chatMapper.getGroupByName(groupName);
        if (group == null) return -1;
        List<Group> groupList = getGroup(userName);
        for (Group groupi : groupList) {
            if (groupi.getName().equals(group.getName())) return 0;
        }
        chatMapper.insertGroup(groupName, userName);
        return 1;
    }

    /**
     * @param chatName
     * @param userName
     * @return
     */
    public List<Chat> getChatLog(String chatName, String userName) {
        User user = chatMapper.getUserByName(chatName);
        if(user != null) {
            List<Chat> chatLog1 = chatMapper.getChatLog(chatName, userName);
            List<Chat> chatLog2 = chatMapper.getChatLog(userName, chatName);
            chatLog1.addAll(chatLog2);
            Collections.sort(chatLog1, (chat1, chat2) -> Integer.compare(chat1.getChatId(), chat2.getChatId()));
            return chatLog1;
        }
        else{
            return chatMapper.getGroupLog(chatName);
        }

    }
}
